#ifndef TCP_SERVER_HTTPCONTEXT_H
#define TCP_SERVER_HTTPCONTEXT_H

#include "../../base/copyable.h"
#include "HttpRequest.h"

class Buffer;

class HttpContext : public muduo::copyable {
public:
    enum HttpRequestParseState {
        kExpectRequestLine,
        kExpectHeaders,
        kExpectBody,
        kGotAll
    };

    HttpContext() : state_(kExpectRequestLine) {}

    /**
     * 状态机解析HTTP请求
     * @param buf
     * @param receiveTime
     * @return
     */
    bool parseRequest(Buffer *buf, Timestamp receiveTime);

    bool gotAll() const { return state_ == kGotAll; }

    void reset() {
        state_ = kExpectRequestLine;
        HttpRequest dummy;
        request_.swap(dummy);
    }

    const HttpRequest &request() const { return request_; }

    HttpRequest &request() { return request_; }

private:
    bool processRequestLine(const char *begin, const char *end);

    HttpRequestParseState state_;
    HttpRequest request_;
};


#endif //TCP_SERVER_HTTPCONTEXT_H
